{% extends '_base_list.html' %}
{% load i18n static %}

{% block help_message %}
    <div class="alert alert-info help-message">
    这里的导航-也就是应用，授权应用的用户和用户组，也就是授权哪些用户和用户组可以直接登录应用。
    </div>
{% endblock %}

{% block table_search %}
{% endblock %}
{% block table_container %}
<div class="uc pull-left m-r-5"><a href="{% url "navis:navi-create" %}" class="btn btn-sm btn-primary"> {% trans "Create navi" %} </a></div>
<table class="table table-striped table-bordered table-hover " id="user_list_table" >
    <thead>
        <tr>
            <th class="text-center">
                <input id="" type="checkbox" class="ipt_check_all">
            </th>
            <th class="text-center">{% trans 'Name' %}</th>
            <th class="text-center">{% trans 'Url' %}</th>
            <th class="text-center">{% trans 'Action' %}</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
{#<div id="actions" class="hide">#}
{#    <div class="input-group">#}
        {#<select class="form-control m-b" style="width: auto" id="slct_bulk_update">#}
        {#    <option value="delete">{% trans 'Delete selected' %}</option>  #}
        {#    <option value="update">{% trans 'Update selected' %}</option>  #}
        {#    <option value="deactive">{% trans 'Deactive selected' %}</option>#}
        {#    <option value="active">{% trans 'Active selected' %}</option>#}
        {#</select>#}
{#        <div class="input-group-btn pull-left" style="padding-left: 5px;">#}
{#            <button id='btn_bulk_update' style="height: 32px;"  class="btn btn-sm btn-primary">#}
{#             {% trans 'Submit' %}#}
{#            </button>#}
{#        </div>#}
{#    </div>#}
{#</div>#}
{% endblock %}
{% block content_bottom_left %}{% endblock %}
{% block custom_foot_js %}
<script src="{% static 'js/jquery.form.min.js' %}"></script>
<script>

function initTable() {
     var options = {
        ele: $('#user_list_table'),
        columnDefs: [
            {targets: 1, createdCell: function (td, cellData, rowData) {
                var detail_btn = '<a href="{% url "navis:navi-detail" pk=DEFAULT_PK %}">' + cellData + '</a>';
                $(td).html(detail_btn.replace("{{ DEFAULT_PK }}", rowData.id));
             }},

            {targets: 3, createdCell: function (td, cellData, rowData) {
                var update_btn = '<a href="{% url "navis:navi-update" pk=DEFAULT_PK %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'.replace('00000000-0000-0000-0000-000000000000', cellData);

                var del_btn = "";

                del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_user_delete" data-uid="{{ DEFAULT_PK }}" data-name="99991938">{% trans "Delete" %}</a>'
                            .replace('{{ DEFAULT_PK }}', cellData)
                            .replace('99991938', rowData.name);

                $(td).html(update_btn + del_btn)
             }}],
        ajax_url: '{% url "api-navis:navi-list" %}',
        columns: [
            {data: "id"}, {data: "name" }, {data: "url" }, {data: "id" }
        ],
        op_html: $('#actions').html()
    };
    table = jumpserver.initDataTable(options);
    return table
}

$(document).ready(function(){
    var table = initTable();
    var fields = $('#fm_user_bulk_update .form-group');
    $.each(fields, function (index, value) {
        console.log(value)
    });

}).on('click', '#btn_bulk_update', function(){
    var action = $('#slct_bulk_update').val();
    var $data_table = $('#user_list_table').DataTable();
    var id_list = [];
    var plain_id_list = [];
    $data_table.rows({selected: true}).every(function(){
        id_list.push({pk: this.data().id});
        plain_id_list.push(this.data().id);
    });
    if (id_list === []) {
        return false;
    }
    var the_url = "{% url 'api-navis:navi-list' %}";
    function doDeactive() {
        var body = $.each(id_list, function(index, user_object) {
            user_object['is_active'] = false;
        });
        function success() {
            location.reload();
        }
        APIUpdateAttr({
            url: the_url,
            method: 'PATCH',
            body: JSON.stringify(body),
            success: success
        });
        location.reload();
    }
    function  doActive() {
        var body = $.each(id_list, function(index, user_object) {
            user_object['is_active'] = true;
        });
        function success() {
            location.reload();
        }
        APIUpdateAttr({
            url: the_url,
            method: 'PATCH',
            body: JSON.stringify(body),
            success: success
        });
    }
    function doDelete() {
        swal({
            title: "{% trans 'Are you sure?' %}",
            text: "{% trans 'This will delete the selected navis !!!' %}",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "{% trans 'Confirm' %}",
            closeOnConfirm: false
        }, function() {
            var success = function() {
                var msg = "{% trans 'Navi Deleted.' %}";
                swal("{% trans 'Navi Delete' %}", msg, "success");
                $('#user_list_table').DataTable().ajax.reload();
            };
            var fail = function() {
                var msg = "{% trans 'Navi Deleting failed.' %}";
                swal("{% trans 'Navi Delete' %}", msg, "error");
            };
            var url_delete = the_url + '?id__in=' + JSON.stringify(plain_id_list);
            APIUpdateAttr({url: url_delete, method: 'DELETE', success: success, error: fail});
            jumpserver.checked = false;
        });
    }
    function doUpdate() {
        var users_id = plain_id_list.join(',');
        var url = "{% url 'users:user-bulk-update' %}?users_id=" + users_id;
        location.href = url
    }
    switch(action) {
        case 'deactive':
            doDeactive();
            break;
        case 'delete':
            doDelete();
            break;
        case 'update':
            doUpdate();
            break;
        case 'active':
            doActive();
            break;
        default:
            break;
    }
}).on('click', '.btn_user_delete', function(){
    var $this = $(this);
    var name = $this.data('name');
    var uid = $this.data('uid');
    var the_url = '{% url "api-navis:navi-detail" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", uid);
    objectDelete($this, name, the_url);
})
</script>
{% endblock %}

